package com.example.rabbitmq.ksdemo;

import com.example.rabbitmq.ksdemo.Thread.ThreadManager;
import com.example.rabbitmq.ksdemo.callback.MyConsumer;
import com.example.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.*;
import lombok.SneakyThrows;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class Recv {

    private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(
            15, 30, 200, TimeUnit.MILLISECONDS,
            new ArrayBlockingQueue<>(1000));

    public static void main(String[] args){

        try {
            Connection connection = ConnectionUtil.getConnection();


            for (int i = 0; i < 5; i++) {
                //线程池
                executor.execute(()->{
                    try {
                        final Channel channel = connection.createChannel();
                        Map<String, Object> map = new HashMap();
                        map.put("x-queue-type","classic");
                        channel.queueDeclare("ks-queue", true, false, false, map);
                        channel.basicQos(1);


                        MyConsumer consumer=new MyConsumer(channel,new ThreadManager());
                        channel.basicConsume("ks-queue",false,consumer);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });

                //手动线程
             /*   Thread thread=new Thread(new Runnable(){
                    @SneakyThrows
                    @Override
                    public void run() {
                        final Channel channel = connection.createChannel();
                        Map<String, Object> map = new HashMap();
                        map.put("x-queue-type","classic");
                        channel.queueDeclare("ks-queue", true, false, false, map);
                        channel.basicQos(1);


                        MyConsumer consumer=new MyConsumer(channel,new ThreadManager());
                        channel.basicConsume("ks-queue",false,consumer);
                    }
                });
                thread.start();*/
            }

        } catch (IOException e) {
            e.printStackTrace();

        } catch (TimeoutException e) {
            e.printStackTrace();
        }

    }
}
